home *** CD-ROM | disk | FTP | other *** search
- Path: lerc.nasa.gov!purdue!yuma!steffend
- From: steffend@lamar.colostate.edu (Dave Steffen)
- Newsgroups: comp.lang.c++
- Subject: Re: Trouble w/ random numbers...please help
- Date: 4 Feb 1996 02:26:15 GMT
- Organization: Colorado State University, Fort Collins, CO 80523
- Message-ID: <4f15g7$2sl4@yuma.ACNS.ColoState.EDU>
- References: <4f0g5u$879@ns.campus.mci.net>
- NNTP-Posting-Host: glitch.physics.colostate.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Daniel Cotter (cotter@www.cns.uky.edu) wrote:
- > I'm trying to write a program that will generate a set of
- > random numbers between 3 and 18. I have several problems with
- > this program.
- > 1. C++ always generates same number(346)
- > 2. I don't know how to give a range. I've tried 3-18, but
- > it thinks I mean to subtract(3-18=15)
- > 3. When it does work, it's always the same number
-
- OK, first of all: computers are deterministic machines, so
- they can't generate truly random number sequences without special
- hardware.
- Typically, "random number generators" are functions that
- generate a sequence of numbers, but they need some number to start
- from. This number is called a "seed". For a given seed, the "random"
- numbers generated will always be the _same_ random numbers - but
- they'll be statistically uncorrelated _with each other_.
-
- My guess is that you're not initializing your random number,
- so the seed is some default value; thus you'll always get the same
- sequence. A common way to initialize the generator is to create a seed
- value based on the current date and time, thereby generating a
- different seed each time the program is run.
-
- As for the other problems, check your documentation for the
- rand() and/or srand() routines. They're usually not that good, but
- they'll get you started. If you need better routines, check "Numerical
- Recipes in C, 2nd Ed".
-
- /\
- \/
-
- Dave Steffen No, his mind is not for rent
- Dept. of Physics To any God or Government
- Colorado State University Always hopeful, yet discontent
- steffend@lamar.colostate edu He knows changes aren't permanent-
- But change is...
- "Speak softly...
- ... and carry a black belt!" -Neal Peart / RUSH
- -----------------------------------------------------------------------
-